package com.socool.sinofaith.ui;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

import com.socool.sinofaith.SocoolApplication;
import com.socool.sinofaith.view.ChooseDialog;
import com.socool.sinofaith.view.ChooseDialog.OnDialogClickListener;

public class UseMoreCameraActivity extends Activity {
	private String mImageFilePath;
	public static final String IMAGEFILEPATH = "ImageFilePath";
	public final static String IMAGE_PATH = "image_path";
	static Activity mContext;
	public final static int GET_IMAGE_REQ = 5000;
	private int REQUEST_CODE_CAPTURE_CAMEIA = 200;
	private static Context applicationContext;
	private ChooseDialog mDialog;
	private int index=0;
	private String morePath="";
	private Intent rsl ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SocoolApplication.addActivity(this);
		mContext = this;
		mDialog=new ChooseDialog(this);
		rsl= new Intent();
		applicationContext = getApplicationContext();
		if (savedInstanceState == null) {
			initialUI();
		}
		mDialog.setOnDialogClickListener(new OnDialogClickListener() {
			
			@Override
			public void onRightClick() {
				
				morePath+=mImageFilePath+"#";
				Log.i("jsc", "dialog-right:"+morePath);
				initialUI();
			
			}
			
			@Override
			public void onLeftClick() {
				morePath+=mImageFilePath;
				Log.i("jsc","dialog-left:"+ mImageFilePath+"   mImageFilePath");
//			
					rsl.putExtra(IMAGE_PATH, morePath);
					mContext.setResult(REQUEST_CODE_CAPTURE_CAMEIA, rsl);
					mContext.finish();
				
			}
		});

	}

	public void initialUI() {
		//根据时间生成 后缀为  .jpg 的图片
		long ts = System.currentTimeMillis();
		mImageFilePath = getCameraPath() + ts + ".jpg";
		if(mImageFilePath!=null){
			
			File out = new File(mImageFilePath);
			showCamera(out);
		}else{
			Toast.makeText(UseMoreCameraActivity.this, "拍照正在初始化,请退出重新拍照", 1).show();
		}

	}

	private void showCamera(File out) {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out)); // set
		startActivityForResult(intent, GET_IMAGE_REQ);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {

		if (GET_IMAGE_REQ == requestCode && resultCode == Activity.RESULT_OK) {
			index++;
			if(index<4){
				//不为4 .弹出框
				mDialog.setText(index);
			}else {
				//index 为4 直接返回
				morePath+=mImageFilePath;
				rsl.putExtra(IMAGE_PATH, morePath);
				mContext.setResult(REQUEST_CODE_CAPTURE_CAMEIA, rsl);
				mContext.finish();
			}
			
			
			
			

		} else {
			mContext.finish();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("ImageFilePath", mImageFilePath + "");

	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);

	}

	public static String getCameraPath() {
		String filePath = getImageRootPath() + "/camera/";
		File file = new File(filePath);
		if (!file.isDirectory()) {
			file.mkdirs();
		}
		file = null;
		return filePath;
	}

	public static String getImageRootPath() {
		String filePath = getAppRootPath() + "/image";
		File file = new File(filePath);
		if (!file.exists()) {
			file.mkdirs();
		}
		file = null;
		return filePath;
	}

	public static String getAppRootPath() {
		String filePath = "/a";
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			filePath = Environment.getExternalStorageDirectory() + filePath;
		} else {
			filePath = applicationContext.getCacheDir() + filePath;
		}
		File file = new File(filePath);
		if (!file.exists()) {
			file.mkdirs();
		}
		file = null;
		File nomedia = new File(filePath + "/.nomedia");
		if (!nomedia.exists())
			try {
				nomedia.createNewFile();
			} catch (IOException e) {
			}
		return filePath;
	}
}
